<?php
$this->LoadHelper('ApiHelper');
$ApiHelper = new ApiHelper();

$this->LoadHelper('BankHelper');
$BankHelper = new BankHelper();

$this->LoadHelper('MemberHelper');
$MemberHelper = new MemberHelper();

$check = $ApiHelper->tokenCheck($this->Param);
if(!$check['success']) ErrorMsg::ApiMsg($check['msg'], $check['code']);

$member_info = $MemberHelper->getMember($this->Param['uid']);
$this->Assign('member_info', $member_info);

switch($this->Param['option']){
    case 'list':
        $where = array(
            '`bk_status` = ?' => 'ok',
            '`bk_id` IN (?)' => empty($member_info['u_bank']) ? array(0) : explode(',', $member_info['u_bank'])
        );
        $data = $BankHelper->bankAll($where);
        $out_data = $data?$data:array();
        /*$out_data = array();
        if(!empty($data)) foreach($data as $k=>$v){
            $out_data[] = array(
                'bk_id' => $v['bk_id'],
                'bk_code' => $v['bk_code'],
                'bk_name' => $v['bk_name'],
                'bk_can_online_verify' => $v['bk_can_online_verify'],
            );
        }*/
        $ApiHelper->logSave('getbank', $this->Param);
        ErrorMsg::ApiMsg('请求成功', 0 , $out_data);
        break;
    case 'bankpro':
        $bank_id = empty($this->Param['o_bank_id']) ? ErrorMsg::ApiMsg('银行ID值错误') : $this->Param['o_bank_id'];
        $loan_year = empty($this->Param['ot_loan_year']) ? ErrorMsg::ApiMsg('贷款年限值错误') : $this->Param['ot_loan_year'];
        $loan_type = empty($this->Param['ot_loan_type']) ? ErrorMsg::ApiMsg('产品类型错误') : $this->Param['ot_loan_type'];
        $province = empty($this->Param['ot_reg_province']) ? ErrorMsg::ApiMsg('车辆上牌地不能为空') : $this->Param['ot_reg_province'];
        $car_tp = empty($this->Param['ot_car_pose']) ? ErrorMsg::ApiMsg('车辆类型不能为空') : $this->Param['ot_car_pose'];
        $params = array(
            'o_bank_id' => $bank_id,
            'ot_loan_type' => $loan_type,
            'ot_loan_year' => $loan_year,
            'ot_reg_province' => $province,
            'ot_car_pose' => $car_tp,
            'oid' => $this->Param['oid']
        );
        $result = $BankHelper->rateBack($params);
        if(is_null($result['data']['company_rate'])){
            $result['data']['company_rate'] = '';
        }
        ErrorMsg::ApiMsg('请求成功', 0 , $result['data']);
        break;
    case 'loanrate':
        $this->Param['ot_reg_province'] = empty($this->Param['ot_reg_province']) ? '浙江省' : $this->Param['ot_reg_province'];
        $ApiHelper->logSave('loanrate', $this->Param); //临时调试
        $result = $BankHelper->rateCompute($this->Param);
        if($result['success']){
            ErrorMsg::ApiMsg('获取成功', 0, $result['data']);
        }else{
            ErrorMsg::ApiMsg($result['msg']);
        }
        break;
    case 'bankinput':
        $rnd_pwd = StringCode::RandomCode(3, 'lower') . StringCode::RandomCode(3, 'num');
        $bank_id = empty($this->Param['o_bank_id']) ? ErrorMsg::ApiMsg('银行ID值错误') : $this->Param['o_bank_id'];
        $bank_row = $BankHelper->bankRow($bank_id);
        $require = array();
        if($bank_row['bk_name'] == '中国农业银行股份有限公司萧山分行'){
            $require[] = 'ot_car_oprice';
        }
        if($bank_row['bk_name'] == '（2017）中国工商银行股份有限公司杭州延中支行'){
            $require[] = 'ot_service_rate';
        }
        ErrorMsg::ApiMsg('获取成功', 0, array('require' => $require));
    default :
        break;
}